package cetcbigdata.da.service.sichuan.service_items;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class SiChuan extends Base {
    private static String baseRegion = "四川省";

    private static String baseModule = "服务事项";

    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "ss", "ss");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        try {
            for(JSONObject r : getRegion()){
                List<JSONObject> moduleList = getModule(r);
                for (JSONObject m : moduleList) {
                    this.parseListPage(recordSender, fieldsList, r, m);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private JSONObject parseListPage(RecordSender recordSender, List<Object> fieldsList, JSONObject regionItem, JSONObject moduleItem) {
        //获取区名和类型名称
        String region = baseRegion + "-" + regionItem.getString("name");
        String module = baseModule + "-" + moduleItem.getString("name");

        //
        String regCode = regionItem.getString("regCode");
        int pageno = 1;
        try {
            String listUrl = moduleItem.getString("url");
            JSONObject header = new JSONObject();
            JSONObject params = moduleItem.getJSONObject("params");
            //获取params的返回值的个数，进行判断
            int paramsSize = params.size();
            if (paramsSize == 1){
                try {
                    String response = getReq(listUrl, header, params);
                    response = new String(response.getBytes("iso-8859-1"), "utf-8");
                    JXDocument jxDocument = new JXDocument(response);
                    List<Object> rs = jxDocument.sel("//div[@class='wd1200 cf']//ul[@class='bm-list']");
                    for (Object o : rs) {
                        JSONObject content = new JSONObject();
                        String name = ((Element) o).child(0).child(0).text();
                        String detailUrl = ((Element) o).child(0).child(0).attr("href");
                        content.put(name, detailUrl);
                        processResult(
                                listUrl, response, detailUrl, "", content, region, module
                        );
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else if (paramsSize > 2 && paramsSize <= 5){
                try {
                    //没有数据返回时，网站中会出现 div[@class='noitem'] 元素，则停止翻页
                    while (true){
                        params.put("pageno", pageno);
                        String response = getReq(listUrl, header, params);
                        JXDocument jxDocument = new JXDocument(response);
                        List<Object> firstTitleList = jxDocument.sel("//div[@class='sx_list']");
                        for (Object o : firstTitleList){
                            JSONObject detailHeader = new JSONObject();
                            JSONObject detailParams = new JSONObject();
                            JSONObject content = new JSONObject();
                            String detailUrl = null;
                            String detailResponse = null;
                            String firstTitle = null;
                            List<JSONObject> xuanxiang = new ArrayList<>();
                            // 判断是否有子选项
                            Elements elements = ((Element) o).getElementsByTag("ul");
                            if(elements.size() > 0){
                                // 有子选项
                                Elements lis = ((Element) o).child(1).children();
                                for(Element li : lis){
                                    JSONObject xuanxiangItem = new JSONObject();
                                    String secondTitle = li.child(0).attr("title");
                                    detailUrl = li.child(0).attr("href");
                                    xuanxiangItem.put(secondTitle, detailUrl);
                                    xuanxiang.add(xuanxiangItem);
                                }
                            }
                            else {
                                JSONObject xuanxiangItem = new JSONObject();
                                firstTitle = ((Element) o).child(0).child(0).attr("title");
                                detailUrl = ((Element) o).child(0).child(0).child(0).attr("href");
                                xuanxiangItem.put("名称", firstTitle);
                                xuanxiangItem.put("网址", detailUrl);
                                xuanxiang.add(xuanxiangItem);

                            }
                            firstTitle = ((Element) o).child(0).child(0).attr("title");
                            content.put("名称", firstTitle);
                            content.put("选项", xuanxiang);
                            processResult(
                                    listUrl, response, detailUrl, "", content, region, module
                            );
                        }
                        if (jxDocument.sel("//div[@class='sx_list']").size() == 0){
                            break;
                        }
                        pageno ++;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else{
                try {
                    //没有数据返回时，网站中会出现 div[@class='noitem'] 元素，则停止翻页
                    while (true){
                        params.put("pageno", pageno);
                        String response = getReq(listUrl, header, params);
                        JXDocument jxDocument = new JXDocument(response);
                        List<Object> firstTitleList = jxDocument.sel("//div[@class='sx_list']");
                        for (Object o : firstTitleList){
                            JSONObject detailHeader = new JSONObject();
                            JSONObject detailParams = new JSONObject();
                            JSONObject content = new JSONObject();
                            String detailUrl = null;
                            String detailResponse = null;
                            String firstTitle = null;
                            List<JSONObject> xuanxiang = new ArrayList<>();
                            // 判断是否有子选项
                            Elements elements = ((Element) o).getElementsByTag("ul");
                            if(elements.size() > 0){
                                // 有子选项
                                Elements lis = ((Element) o).child(1).children();
                                for(Element li : lis){
                                    JSONObject xuanxiangItem = new JSONObject();
                                    String secondTitle = li.child(0).attr("title");
                                    detailUrl = li.child(0).attr("href");
                                    xuanxiangItem.put(secondTitle, detailUrl);
                                    xuanxiang.add(xuanxiangItem);
                                }
                            }
                            else {
                                JSONObject xuanxiangItem = new JSONObject();
                                firstTitle = ((Element) o).child(0).child(0).attr("title");
                                detailUrl = ((Element) o).child(0).child(0).child(0).attr("href");
                                xuanxiangItem.put("名称", firstTitle);
                                xuanxiangItem.put("网址", detailUrl);
                                xuanxiang.add(xuanxiangItem);

                            }
                            firstTitle = ((Element) o).child(0).child(0).attr("title");
                            content.put("名称", firstTitle);
                            content.put("选项", xuanxiang);
                            System.out.println(content + "===" + region + "-------" + module);
                            processResult(
                                    listUrl, response, detailUrl, "", content, region, module
                            );
                        }
                        if (jxDocument.sel("//div[@class='sx_list']").size() == 0){
                            break;
                        }
                        pageno ++;
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    //获取市名和各市编码
    private static JSONArray getCityList(){
        String city = " [\n" +
                "{ name: '阿坝藏族羌族自治州', value: '513200000000', areaId: '949' },\n" +
                "{ name: '巴中市', value: '511900000000', areaId: '1995' },\n" +
                "{ name: '成都市', value: '510100000000', areaId: '1447' },\n" +
                "{ name: '达州市', value: '511700000000', areaId: '504' },\n" +
                "{ name: '德阳市', value: '510600000000', areaId: '1499' },\n" +
                "{ name: '甘孜藏族自治州', value: '513300000000', areaId: '1512' },\n" +
                "{ name: '广安市', value: '511600000000', areaId: '1709' },\n" +
                "{ name: '广元市', value: '510800000000', areaId: '408' },\n" +
                "{ name: '乐山市', value: '511100000000', areaId: '890' },\n" +
                "{ name: '凉山彝族自治州', value: '513400000000', areaId: '329' },\n" +
                "{ name: '泸州市', value: '510500000000', areaId: '774' },\n" +
                "{ name: '眉山市', value: '511400000000', areaId: '1419' },\n" +
                "{ name: '绵阳市', value: '510700000000', areaId: '1746' },\n" +
                "{ name: '内江市', value: '511000000000', areaId: '1588' },\n" +
                "{ name: '南充市', value: '511300000000', areaId: '1199' },\n" +
                "{ name: '攀枝花市', value: '510400000000', areaId: '386' },\n" +
                "{ name: '遂宁市', value: '510900000000', areaId: '1477' },\n" +
                "{ name: '雅安市', value: '511800000000', areaId: '1028' },\n" +
                "{ name: '宜宾市', value: '511500000000', areaId: '1996' },\n" +
                "{ name: '资阳市', value: '512000000000', areaId: '1011' },\n" +
                "{ name: '自贡市', value: '510300000000', areaId: '661' }\n" +
                "]";
        return JSONArray.parseArray(city);
    }

    public static List<JSONObject> getRegion(){
        //获取各市编码
        JSONArray cityList = getCityList();
        List<JSONObject> out = new ArrayList<>();
        for (Object o : cityList){
            JSONObject oo = (JSONObject) o;
            String code = oo.getString("value");

            //请求获取各市包含的区的名称和编码
            String url = "http://www.sczwfw.gov.cn/jiq/interface/jitem/find-by-code?code=" + code + "&deptType=1&jsonpCallback=jsonpcallback&_=1664338799123";
            JSONObject header = new JSONObject();
            JSONObject params = new JSONObject();

            String response = getReq(url, header, params);
            //处理数据获取区名和编码
            String data = response.replace("jsonpcallback", " ").replace("(", " ").replace(")", " ");
            JSONObject responseObj = JSONObject.parseObject(data);
            if(responseObj.getString("code").equals("200")){
                JSONArray resList = responseObj.getJSONObject("data").getJSONObject("group").getJSONArray("sonAreas");
                for (Object r : resList){
                    JSONObject rr = (JSONObject) r;
                    JSONObject outItem = new JSONObject();
                    String name = oo.getString("name") + "-" + rr.getString("name");
                    outItem.put("name", name);
                    outItem.put("regCode", rr.getString("areaCode"));
                    out.add(outItem);
                }
            }
        }
        return out;
    }

    private static List<JSONObject> getModule(JSONObject r){
        try {
            //存储各模块的请求参数
            List<JSONObject> modules = new ArrayList<>();
            JSONObject params1 = new JSONObject();
            JSONObject params2 = new JSONObject();
            JSONObject params3 = new JSONObject();
            JSONObject params4 = new JSONObject();
            JSONObject params5 = new JSONObject();
            JSONObject params6 = new JSONObject();
            JSONObject params7 = new JSONObject();

            JSONObject moduleItem1 = new JSONObject();
            JSONObject moduleItem2 = new JSONObject();
            JSONObject moduleItem3 = new JSONObject();
            JSONObject moduleItem4 = new JSONObject();
            JSONObject moduleItem5 = new JSONObject();
            JSONObject moduleItem6 = new JSONObject();
            JSONObject moduleItem7 = new JSONObject();

            moduleItem1.put("name", "行政权力事项清单");
            moduleItem1.put("url", "http://www.sczwfw.gov.cn/jiq/interface/item/tags");
            params1.put("dxType", 21);
            params1.put("areaCode", r.getString("regCode"));
            params1.put("deptCode", "");
            params1.put("searchtext", "");
            params1.put("taskType", "");
            moduleItem1.put("params", params1);

            moduleItem2.put("name", "责任清单");
            moduleItem2.put("url", "http://www.sczwfw.gov.cn/col/col18206/index.html");
            params2.put("areaCode",r.getString("regCode"));
            moduleItem2.put("params", params2);

            moduleItem3.put("name", "公共服务事项清单");
            moduleItem3.put("url", "http://www.sczwfw.gov.cn/jiq/interface/item/tags");
            params3.put("dxType", 5);
            params3.put("areaCode", r.getString("regCode"));
            params3.put("deptCode", "");
            params3.put("searchtext", "");
            params3.put("taskType", "");
            moduleItem3.put("params", params3);

            moduleItem4.put("name", "通办事项清单");
            moduleItem4.put("url", "http://www.sczwfw.gov.cn/jiq/interface/item/tags");
            params4.put("dxType", 16);
            params4.put("areaCode", r.getString("regCode"));
            params4.put("deptCode", "");
            params4.put("searchtext", "");
            params4.put("taskType", "");
            moduleItem4.put("params", params4);

            moduleItem5.put("name", "通用目录清单");
            moduleItem5.put("url", "http://www.sczwfw.gov.cn/jiq/interface/item/tags");
            params5.put("dxType", 9);
            params5.put("areaCode", r.getString("regCode"));
            params5.put("deptCode", "");
            params5.put("searchtext", "");
            params5.put("taskType", "");
            moduleItem5.put("params", params5);

            moduleItem6.put("name", "跑腿次数清单-跑一次");
            moduleItem6.put("url", "http://www.sczwfw.gov.cn/jiq/interface/item/tags");
            params6.put("dxType", 52);
            params6.put("areaCode", r.getString("regCode"));
            params6.put("deptCode", "");
            params6.put("searchtext", "");
            params6.put("taskType", "");
            params6.put("limitSceneNum", 1);
            moduleItem6.put("params", params6);

            moduleItem7.put("name", "跑腿次数清单-零跑腿");
            moduleItem7.put("url", "http://www.sczwfw.gov.cn/jiq/interface/item/tags");
            params7.put("dxType", 52);
            params7.put("areaCode", r.getString("regCode"));
            params7.put("deptCode", "");
            params7.put("searchtext", "");
            params7.put("taskType", "");
            params7.put("limitSceneNum", 0);
            moduleItem7.put("params", params7);

            modules.add(moduleItem1);
            modules.add(moduleItem2);
            modules.add(moduleItem3);
            modules.add(moduleItem4);
            modules.add(moduleItem5);
            modules.add(moduleItem6);
            modules.add(moduleItem7);

            System.out.println(modules);
            return modules;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public static void main(String[] args) {
        SiChuan spider = new SiChuan();
        spider.getData(null, new ArrayList<>());
    }
}
